package com.crm.service.impl;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.crm.model.User;
import com.crm.service.IRedisCacheService;
import com.crm.service.IUserService;
import com.crm.uilt.RedisUtil;
import io.netty.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zjd
 * @Title:
 * @Description:
 * @date 2021/4/79:44
 */
@Service
public class RedisCacheServiceImpl implements IRedisCacheService {
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * 生成键 user::com.crm.service.impl.RedisCacheServiceImpl.findUser.1
     * */
    @Override
    @Cacheable("user")
    public User findUser(String userId) {
        User user = userService.getById(userId);
        return user;
    }

    /**
     *  多个键
     * 生成键 user::com.crm.service.impl.RedisCacheServiceImpl.findUserTwo.1
     *       userId::com.crm.service.impl.RedisCacheServiceImpl.findUserTwo.1
     * */
    @Override
    @Cacheable({"user","userId"})
    public User findUserTwo(String userId) {
        User user = userService.getById(userId);
        return user;
    }

    /**
     * 生成键 user::1
     * */
    @Override
    @Cacheable(cacheNames="user", key="#userId")
    public User findUserThree(String userId) {
        User user = userService.getById(userId);
        return user;
    }

    /**
     *   @Cacheable(cacheNames="book", condition="#name.length() < 32")
     *   @Cacheable(cacheNames="book", condition="#name.length() < 32", unless="#result.hardback")
     * */
    @Override
    //@Cacheable(cacheNames="user", condition="#userId.length <= 2 ")
    public User findUserFore(String userId) {
        User user = userService.getById(userId);
        return user;
    }

    @Override
    @CachePut(cacheNames="user", key="#user.id")
    public User update(User user) {
         userService.updateById(user);
         return userService.getById(user.getId());
    }

    @Override
    @CacheEvict(value = "user",allEntries = true)
    public void clena() {

    }


    @Override
    public String createToken (){
        String str = RandomUtil.randomUUID();
        StrBuilder token=new StrBuilder();
        try {
            token.append("TOKEN_PREFIX").append (str);
            redisUtil.setEx(token.toString(),token.toString(),10000L);
            boolean notEmpty= StrUtil.isNotEmpty( token.toString());
            if(notEmpty){
                return token.toString();
            }
        }
        catch(Exception ex){
            ex.printStackTrace ();
        }
        return  null;
    }



    @Override
    public boolean checkToken(HttpServletRequest request) throws Exception {
        String token=request.getHeader("TOKEN_NAME");
        if(StrUtil.isBlank(token)){
                // header中不存在token
            token =request.getParameter("TOKEN_NAME");
            if(StrUtil.isBlank (token)) {
            // parameter中也不存在token
                throw new Exception();
            }
        }
        if (!redisUtil.exists(token)) {
            throw new Exception();
        }
        boolean remove = redisUtil.remove(token);
        if(!remove){
            throw new Exception();
        }
        return true;
    }

}
